import util from '@/libs/util';
import Cookies from 'js-cookie';

export default {
    name: 'exportable-table',
    props: {
        classId: ''
    },
    data () {
        return {
            columnsClass: [{
                title: '#',
                type: 'selection',
                width: 60
            }, {
                title: '班级名称',
                key: 'className'
            }, {
                title: '学员姓名',
                key: 'studentName'
            }, {
                title: '状态',
                key: 'status',
                render: function (h, params) {
                    let text = '';
                    let status = params.row.status;
                    if (status == '') {
                        text = '未上';
                    } else if (status == 1) {
                        text = '签到';
                    } else if (status == 2) {
                        text = '请假';
                    } else if (status == 3) {
                        text = '迟到';
                    }
                    return h('div', {}, text);
                }
            }, {
                title: '操作',
                key: 'action',
                width: 250,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    let obj = [];
                                    obj.push(params.row.id);
                                    this.doApproveA(1, obj);
                                }
                            }
                        }, '签到'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    let obj = [];
                                    obj.push(params.row.id);
                                    this.doApproveB(2, obj);
                                }
                            }
                        }, '请假'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    let obj = [];
                                    obj.push(params.row.id);
                                    this.doApproveC(3, obj);
                                }
                            }
                        }, '迟到')
                    ]);
                }
            }],

            modelVisible: false,
            selection: [], // 选中的行
            data: [],
            formData: {}, // 新增/编辑表单数据
            pageSize: 5, // 分页数据
            pageIndex: 1, // 分页数据
            total: 0,
            baseuser: JSON.parse(Cookies.get('baseUser'))
        };
    },
    watch: {
        classId (newVal, oldVal) {
            if (newVal) {
                this.loadList();
            }
        }
    },
    mounted () {
        this.loadList();
    },
    methods: {
        loadList () {
            this.modelVisible = true;
            // debugger;
            util.ajax.post('busine/classes/schedule/detail/listStudent', util.toFormData({
                id: this.classId,
                page: this.pageIndex,
                size: this.pageSize,
                classStartTime: this.formData.classStartTime
            })).then(res => {
                if (res.data.data) {
                    this.data = res.data.data.list;
                    this.total = res.data.data.total;
                    console.log(this.classId);
                }
            });
        },
        doApproveA: function (status, id) {
            this.approve(status, id);
        },
        doApproveB: function (status, id) {
            this.approve(status, id);
        },
        doApproveC: function (status, id) {
            this.approve(status, id);
        },
        doApproveBath: function () {
            let list = this.selection;
            if (list.length == 0) {
                this.$Message.error('请选择签到项目');
                return;
            }
            var id = [];
            for (let i in list) {
                id.push(list[i].id);
            }
            this.approve(1, id);
        },
        approve: function (status, id) { // flag 1是签到2是请假3是迟到
            util.ajax.post('busine/classes/schedule/detail/updateStatus', util.toFormData({
                id: id.join(','),
                status: status
            })).then(res => {
                if (res.data.message) {
                    this.$Message.success('操作成功!');
                } else {
                    this.$Message.error('操作失败!');
                }
                this.loadList();
            });
        },
        whenSelectRow: function (selection) {
            this.selection = selection;
        }

    }
};